<?php
/**
 * Created by PhpStorm.
 * User: bg
 * Date: 2017/9/8 0008
 * Time: 下午 1:37
 */

namespace app\admin\controller;

use think\Cache;
use app\common\model\Configuration;
use think\Db;
use think\Exception;

/*
 * 系统设置
 */
class Systemset extends Base{

    protected $config = '';
    public function __construct(){
        parent::__construct();
        $this->config = new Configuration();
    }
    /*
     * 系统参数设置
     */
    public function index(){
        if(request()->post()){
            $value =  $this->config->getkeyjson('defaultinfo');
            if(!$value){
                alert('error', lang('PARAMETER_ERROR'), url('Systemset/index'));
            }
            DB::startTrans();
            try {

                $data = input('');
                $file = request()->file('logo');
                if ($file) {
                    $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                    $data['logo'] = $info->getSaveName();
                }
                if ($value->logo && !isset($data['logo'])) {
                    $data['logo'] = $value->logo;
                }
                if (isset($data['pagesize'])) {
                    $this->config->updatekey('pagesize', $data['pagesize']);
                    unset($data['pagesize']);
                }
                $ret = $this->config->updatekey('defaultinfo', json_encode($data));
                Cache::clear();
                Db::commit();
                alert('success', lang('SUCCESSFUL_OPERATION'), url('Systemset/index'));
            }catch (Exception $e){
                Db::rollback();
                alert('error', lang('OPERATION_FAILED'), url('Systemset/index'));
            }
        }else{
            $value =  $this->config->getkeyjson('defaultinfo');
            $pagesize =  $this->config->getkey('pagesize');
            $sdata =  collection($value)->toArray();
            $sdata['pagesize'] =$pagesize;
            $this->view->data = $sdata;
            $this->view->alert = parseAlert();
            return $this->fetch();
        }
    }

    /*
     * 微信参数设置
     */
    public function weixin(){
        $data = $this->config->getkey('wx_config');
        $this->assign('data',json_decode($data,true));
        return $this->fetch();
    }

    /*
     * 短信/smtp设置
     */
    public function channel(){
        $smtp_data = $this->config->getkey('smtp');//邮件配置
        $sms_data = $this->config->getkey('sms');//短信配置
        $this->assign('smtp',json_decode($smtp_data,true));
        $this->assign('sms',json_decode($sms_data,true));
        return $this->fetch();
    }
    /*
    *修改配置
    */
    public function edit(){
        if(empty(input('param.name'))){
            return json(['code'=>-1,'msg'=>'缺少参数name']);
        }
        if(input('param.name')=='smtp'){
            $param['MAIL_ADDRESS']= input('param.MAIL_ADDRESS');
            $param['MAIL_SMTP']= input('param.MAIL_SMTP');
            $param['MAIL_PORT']= input('param.MAIL_PORT');
            $param['MAIL_LOGINNAME']= input('param.MAIL_LOGINNAME');
            $param['MAIL_PASSWORD']= input('param.MAIL_PASSWORD');
            $param['MAIL_SECURE']= input('param.MAIL_SECURE');
            if(!inge_isemail($param['MAIL_ADDRESS'])){
                 return json(['code'=>-1,'msg'=>'请输入正确的邮箱地址']);
            }
        }elseif(input('param.name')=='sms'){
            $param['uid']= input('param.uid');
            $param['passwd']= input('param.passwd');
            $param['sign_name']= input('param.sign_name');
            $param['sign_sysname']= input('param.sign_sysname');
            $param['name']= input('param.name');

        }elseif(input('param.name')=='wx_config'){
            $param['app_id']= input('param.app_id');
            $param['app_secret']= input('param.app_secret');
        }

        $arr['value'] = json_encode($param);
        $isret = $this->config->getIskey(input('param.name'));
        if(!$isret){
            $data['name'] =  input('param.name');
            $data['value'] =  json_encode($param);
            $res = $this->config->add($data);
        }else{
            $res = $this->config->updateData(input('param.name'),$arr);
        }
        if($res){
            return json(['code'=>1,'msg'=>'修改成功']);
        }else{
            return json(['code'=>-1,'msg'=>'未做任何修改']);
        }

    }
    //发送邮件
    public function sendEmail(){
        $email = input('param.email');
        if(!inge_isemail($email)){
            return json(['code'=>-1,'msg'=>'请输入正确的邮箱地址']);
        }
        $desc_content = "邮件发送接口测试成功";
        $title = "邮件接口测试";
        $res = sendEmail($desc_content, $email,$title);
        if($res==1){
            return json(['code'=>1,'msg'=>'邮件发送成功,请输入查收']);
        }else{
            return json(['code'=>-1,'msg'=>'邮件发送失败']);
        }


    }
    //发送短信
    public function sendsms(){
        $tel = input('param.tel');
        if(!inge_ismobile($tel)){
            return json(['code'=>-1,'msg'=>'请输入正确的手机号']);
        }
        $desc_content = "短信发送接口测试成功";
        $res = sendSms($tel, $desc_content);
        if($res==1){
            return json(['code'=>1,'msg'=>'短信发送成功,请输入查收']);
        }else{
            return json(['code'=>-1,'msg'=>'短信发送失败']);
        }
    }

    /*
     * 数据库备份
     */
    public function databack(){
        return $this->fetch();
    }

    /*
     * 清理缓存
     */
    public function cleanruntime(){
        remove_dir(RUNTIME_PATH);
        Cache::clear();
        remove_dir(LOG_PATH);
        return json(['code'=>1,'msg'=>'缓存清理成功']);
    }

}